dojo.provide("lorelei.articles.view");
// Lorelei articles view page script
// Copyright (c)2009 Logue <http://logue.be/> All rights reserved.

// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.

// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU General Public License for more details.

// You should have received a copy of the GNU General Public License
// along with this program.  If not, see <http://www.gnu.org/licenses/>.
	
lorelei.articles = {
	meta: {
		"@prefix": "<http://purl.org/net/ns/doas#>",
		"@about": "<http://mabinogi.logue.be/js/lolerei/articles.js>", a: ":JavaScript",
		 title: "Lorelei Article page script",
		 created: "2009-08-17", release: {revision: "1.0", created: "2009-10-10"},
		 author: {name: "Logue", homepage: "<http://logue.be/>"},
		 license: "<http://www.gnu.org/licenses/gpl-3.0.html>"
	},
	id: id,
	user: user,
	auth_id: auth_id,
	view : function(){
		dojo.require("lorelei.mml.player");
		// ナビゲーターのテンプレ
		new dijit.Toolbar({},dojo.byId('navigator'));

		dojo.byId('navigator').innerHTML = [
			'<button id="play"></button>',
			'<button id="stop"></button>',
			'<input type="radio" name="PlayType" id="midi" checked="checked" /><label for="midi">MIDI</label><input type="radio" name="PlayType" id="jsmml" /><label for="jsmml">JSMML</label><input type="radio" name="PlayType" id="sion" disabled="disabled" /><label for="sion">SiON</label>',
			'<button id="export"></button>'
		].join('');
		
		if (auth_id == lorelei.articles.user) dojo.byId('navigator').innerHTML += '<button id="manage"></button>';

		// 再生ボタン
		lorelei.mml.player.PlayButton = new dijit.form.Button({
			id: 'play',
			disabled: false,
			label:locale.play,
			iconClass: 'LoreleiIcon LoreleiIconPlayerPlay'
		},'play');

		// 停止ボタン
		lorelei.mml.player.StopButton = new dijit.form.Button({
			id: 'stop',
			disabled: false,
			label:locale.stop,
			iconClass: 'LoreleiIcon LoreleiIconPlayerStop'
		},'stop');

		// 演奏方式選択ラジオボタン
		dojo.query("input[type='radio']").forEach(function(elem){
			var radio = new dijit.form.RadioButton({
				name:elem.name,
				checked:elem.checked
			},elem);
		});

		// エクスポートメニュー
		var export_menu = new dijit.Menu({
			style: "display: none;"
		});

		// サポートしているエクスポート形式の設定
		// (設置URL)/articles/export/(ID)/メソッド
		// メソッド:[アプリケーション名,アイコンクラス名]
		var export_method = {
			'mms'	: ['mmSeq','LoreleiIconExportMms'],
			'mle'	: ['3ML Editor','LoreleiIconExportMle'],
			'tms'	: ['Text Music Sakura','LoreleiIconExportTms'],
			'tss'	: ['SiON','LoreleiIconExportTss']
		};
		dojo.require("dojox.string.sprintf");
		for (var type in export_method){
			var menu_item = new dijit.MenuItem({
				label: dojox.string.sprintf(locale.download, export_method[type][0]),
				iconClass: 'LoreleiIcon '+export_method[type][1],
				value: type,
				onClick: function(elem){
					console.log(this.value);
//					dojo.byId('export').disabled = true;
					window.open('../../mmls/export/'+lorelei.articles.id+'/'+this.value);
//					dojo.byId('export').disabled = false;
				}
			});
			export_menu.addChild(menu_item);
		}
		
		var export_button = new dijit.form.DropDownButton({
			id:'ExportMenu',
			iconClass: 'LoreleiIcon LoreleiIconExport',
			dropDown: export_menu,
			label : locale.export_file
		},'export');
	
		
		if (auth_id == lorelei.articles.user){
			//管理メニュー
			var manage_menu = new dijit.Menu({
				style: "display: none;"
			});

			// 編集
			manage_menu.addChild(new dijit.MenuItem({
				label: locale['edit track'],
				iconClass: 'LoreleiIcon LoreleiIconManageEdit',
				value: 'edit',
				onClick: function(elem){
					location.href = '../edit/'+lorelei.articles.id;
				}
			}));
			// 削除
			manage_menu.addChild(new dijit.MenuItem({
				label: locale['delete track'],
				iconClass: 'LoreleiIcon LoreleiIconManageEdit',
				value: 'delete',
				onClick: function(elem){
					if( window.confirm(locale.confirm) == true) location.href = '../delete/'+ lorelei.articles.id;
				}
			}));
			
			// トラック追加
			manage_menu.addChild(new dijit.MenuItem({
				label: locale['add track'],
				iconClass: 'LoreleiIcon LoreleiIconMmlAdd',
				value: 'add track',
				onClick: function(elem){
					location.href = '../../mmls/add/'+lorelei.articles.id;
				}
			}));
			
			var manage_buton = new dijit.form.DropDownButton({
				id:'ManageMenu',
				iconClass: 'LoreleiIcon LoreleiIconManage',
				dropDown: manage_menu,
				label : locale.manage
			},'manage');
		}

		// 再生エリア
		lorelei.mml.player.Container = dojo.byId('media');
		
		// 演奏方式選択ラジオボタンイベント割り当て
		dojo.query("input[name='PlayType']").connect('onclick',function(item){
			lorelei.mml.player.selector(item.target.id);
		});
		lorelei.mml.player.id = this.id;
		lorelei.mml.player.init();

		// MMLのトラックごとのボタンの処理。id属性に実行するコマンドとMMLのidが入る
		dojo.query('.mml_btn').connect('onclick', function(elem){
			elem.target.disabled = true;
			var request = elem.target.id.split("_");
			id = request[1];
			switch(request[0]){
				case 'play':
					dojo.byId('media').innerHTML = '<img src="/img/loading_bar.gif" />';
					dojo.require("dojox.embed.Quicktime");
					dojox.embed.Quicktime({ path: '../../mmls/play/'+id,
						id: "player",
						width: 220,
						height: 16
					},dojo.byId('media'));
					elem.target.disabled = false;
				break;
				case 'copy':
					if( window.confirm(message['clipboard']) == true){
						dojo.xhrGet({
							url: '../../mmls/mml/'+id,
							handleAs: "text",
							timeout: 5000, // Time in milliseconds
							// The LOAD function will be called on a successful response.
							load: function(response, ioArgs) {
								dojo.require("lorelei.setClipboard");
								lorelei.setClipboard(response);
								elem.target.disabled = false;
								return response;
							},
							error: function(response, ioArgs) { 
								console.error("dojo.xhrGet: Failure");
								console.log("HTTP status code: ", ioArgs.xhr.status);
								return response;
							}
						});
					}
					elem.target.disabled = false;
				break;
				case 'edit':
					location.href = '../../mmls/edit/'+ id;
				break;
				case 'delete':
					if( window.confirm(message['delete']) == true){
						location.href = '../../mmls/delete/'+ id;
					}else{
						elem.target.disabled = false;
					}
				break;
			}
		});
		
		// タグ削除ボタン
		dojo.query('.LoreleiIconRemove').connect('onclick', function(elem){
			elem.target.disabled = true;
			if( window.confirm(message['delete']) == true){
				location.href = '../remove_tag/'+article_id + '/' + this.value;
			}else{
				elem.target.disabled = false;
			}
		});

		// 記事の翻訳
		var i = 0;
//		lorelei.translate.translate('div#post p');
	}
};

console.log('lorelei.articles.view has been loaded.');

dojo.addOnLoad(function (){
	lorelei.articles.view();
	(function() {
		var dsq = document.createElement('script'); dsq.type = 'text/javascript'; dsq.async = true;
		dsq.src = 'http://lorelei.disqus.com/embed.js';
		(document.getElementsByTagName('head')[0] || document.getElementsByTagName('body')[0]).appendChild(dsq);
	})();
});